package org.zh.test;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Random;

import static org.zh.test.StaticArray.*;


public class BeanRandom {
    private static final Random random = new Random();


    public static String getRandomGender() {
        int randomNum = random.nextInt(2);
        return randomNum == 0 ? "女" : "男";
    }

    //18515730996   11位
    public static String getRandomPhoneNumber() {
        int[] prefixes = {134, 135, 136, 137, 138, 139, 147, 150, 151,
                152, 157, 158, 159, 172, 178, 182, 183, 184, 187, 188, 195,
                197, 198, 130, 131, 132, 145, 155, 156, 166, 175, 176, 185,
                186, 196, 133, 149, 153, 180, 181, 189, 173, 177, 190, 191,
                193, 199, 167, 170, 171, 162, 170, 165, 170};
        int head = prefixes[random.nextInt(prefixes.length)];
        StringBuilder phoneNumber = new StringBuilder();
        phoneNumber.append(head);
        for (int i = 0; i < 8; i++) {
            phoneNumber.append(random.nextInt(10));
        }
        return phoneNumber.toString();
    }

    public static String getRandomManName() {
        return getRandomName("男");
    }

    public static String getRandomFemaleName() {
        return getRandomName("女");
    }

    public static String getRandomName(String gender) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SURNAME[random.nextInt(SURNAME.length - 1)]);
        if (gender.equals("男")) {
            for (int i = 0; i < random.nextInt(1, 3); i++) {
                stringBuilder.append(MALE_NAMES[random.nextInt(FEMALE_NAMES.length)]);
            }
        } else {
            for (int i = 0; i <= random.nextInt(1, 3); i++) {
                stringBuilder.append(FEMALE_NAMES[random.nextInt(FEMALE_NAMES.length)]);
            }
        }
        return stringBuilder.toString();
    }

    public static String getRandomEmail() {
        String emailSuffix = EMAIL_SUFFIX[random.nextInt(EMAIL_SUFFIX.length)];
        String emailPrefix = EMAIL_PREFIXES[random.nextInt(EMAIL_PREFIXES.length)];
        return emailPrefix + emailSuffix;
    }

    /**
     * 随机获取学校名称
     * 该方法从一个JSON文件中读取学校名称列表，并随机返回其中一个学校名称
     * 使用Jackson库中的ObjectMapper来解析JSON文件
     *
     * @return 随机选择的学校名称如果读取或解析过程中发生错误，则返回null
     */
    public static String getRandomSchool() {
        return SCHOOL[random.nextInt(SCHOOL.length)];
    }

    public static LocalDateTime getRandomBirthDayToLocal() {
        Random random = new Random();
        Calendar calendar = Calendar.getInstance();
        // 获取当前年份
        int currentYear = calendar.get(Calendar.YEAR);
        // 生成一个在25到65岁之间的随机年龄
        int age = 25 + random.nextInt(41); // 25 + (0到40) = 25到65
        // 计算出生年份
        int birthYear = currentYear - age;
        // 生成一个随机月份（1到12）
        int birthMonth = 1 + random.nextInt(12);
        // 设置月份
        calendar.set(Calendar.YEAR, birthYear);
        calendar.set(Calendar.MONTH, birthMonth - 1);
        // 生成一个随机日期（1到当月的最大天数）
        int maxDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int birthDay = 1 + random.nextInt(maxDaysInMonth);
        // 设置出生日期
        calendar.set(Calendar.DAY_OF_MONTH, birthDay);
        // 获取LocalDate
        LocalDate birthDate = LocalDate.of(birthYear, birthMonth, birthDay);
        // 返回LocalDateTime，时间部分设置为00:00:00
        return birthDate.atStartOfDay();
    }

    public static Instant getRandomBirthDay() {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = getRandomBirthDayToLocal();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return zonedDateTime.toInstant();
    }

    public static String getRandomSpecialty() {
        return SPECIALTY[random.nextInt(SPECIALTY.length)];
    }

    public static String getRandomEthnicGroup() {
        return ETHNIC_GROUP[random.nextInt(ETHNIC_GROUP.length)];
    }

    public static String getRandomTeachingCertificate() {
        return TEACHING_CERTIFICATE[random.nextInt(TEACHING_CERTIFICATE.length)];
    }

    public static String getRandomEducationalBackground() {
        return EDUCATIONAL_BACKGROUND[random.nextInt(EDUCATIONAL_BACKGROUND.length)];
    }

    public static BigDecimal getRandomSalary() {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return new BigDecimal(decimalFormat.format(random.nextFloat(2500.00F, 100000.00F)));
    }

    public static String getRandomHonnies() {
        return HOBBIES[random.nextInt(HOBBIES.length)];
    }

    public static String getRandomNationality() {
        return NATIONALITY[random.nextInt(NATIONALITY.length)];
    }

    public static String getRandomAddress() {
        return ADDRESSES[random.nextInt(ADDRESSES.length)];
    }

    //411122198910077531 18位
    public static String getRandomIdentityCard() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 18; i++) {
            stringBuilder.append(random.nextInt(10));
        }
        return stringBuilder.toString();
    }

    public static Integer getAge(LocalDateTime birthday) {
        LocalDateTime now = LocalDateTime.now();
        Period period = Period.between(birthday.toLocalDate(), now.toLocalDate());
        return period.getYears();
    }

    public static Integer getAge(Instant birthday) {
        LocalDateTime now = LocalDateTime.now();
        Period period = Period.between(birthday.atZone(ZoneId.systemDefault()).toLocalDate(), now.toLocalDate());
        return period.getYears();
    }

    public static LocalDateTime getRandomOnboardingDate(LocalDateTime birthday) {
        return birthday.plusYears(random.nextInt(24, 35))
                .plusDays(random.nextInt(12)).plusDays(random.nextInt(31));
    }

    public static Instant getRandomOnboardingDate(Instant birthday) {
        return birthday.atZone(ZoneId.systemDefault()).toLocalDateTime().plusYears(random.nextInt(24, 35))
                .plusDays(random.nextInt(12)).plusDays(random.nextInt(31)).atZone(ZoneId.systemDefault()).toInstant();
    }

}
